package com.geese.module.business.controller;

import com.geese.common.BaseController;
import com.geese.common.util.TextValidator;
import com.geese.common.vo.Page;
import com.geese.exception.BusinessException;
import com.geese.module.business.service.IChannelService;
import com.geese.module.business.vo.ChannelInner;
import com.geese.module.business.vo.ChannelVO;
import com.geese.module.user.service.UserService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by Z先生 on 2017/2/27.
 */
@RestController
@RequestMapping(value = "/business/channel")
@Scope("prototype")
public class ChannelController extends BaseController {
    @Autowired
    private UserService userService;
    @Autowired
    private IChannelService channelService;

    /**
     * 渠道列表
     * @param channelVO
     * @param token
     * @return
     */
    @GetMapping("list")
    public String list(ChannelVO channelVO, @RequestParam String token){
//        userService.checkUser(token);
        Page page = getPageInfo(channelVO);
        Map<String, Object>  result = channelService.getChannelList(channelVO, page);
        return response(result);
    }

    /**
     * 获取所有渠道
     * @param token
     * @return
     */
    @GetMapping("all")
    public String list(@RequestParam String token){
        userService.checkUser(token);
        Map<String, Object> result = channelService.getAll();
        return response(result);
    }

    /**
     * 修改渠道
     * @param token
     * @return
     */
    @PostMapping("edit")
    public String edit(@RequestBody ChannelInner channelInner,
                       @RequestParam String token
    ){
//        userService.checkUser(token);
        Integer id = channelInner.getId();
        String username = channelInner.getUsername();
        String mobile = channelInner.getMobile();
        String area = channelInner.getArea();
        if (id == null || id <= 0) {
            throw new BusinessException(120009);
        }
        channelService.updateChannel(id, username, mobile, area);
        return response();
    }

    /**
     * 新增渠道
     * @param token
     * @return
     */
    @PostMapping("add")
    public String add(@RequestBody ChannelInner channelInner,
                      @RequestParam String token
    ){
        String username = channelInner.getUsername();
        String mobile = channelInner.getMobile();
        String area = channelInner.getArea();
//        userService.checkUser(token);
        if (StringUtils.isEmpty(username)) {
            throw new BusinessException(120031);
        }
        if (StringUtils.isEmpty(mobile) || !TextValidator.isMobileExact(mobile)) {
            throw new BusinessException(120032);
        }
        if (StringUtils.isBlank(area)) {
            throw new BusinessException(120032);
        }
        channelService.addChannel(username, mobile, area);
        return response();
    }

    /**
     * 删除渠道一个
     * @param id
     * @param token
     * @return
     */
    @GetMapping("delete")
    public String delete(@RequestParam Integer id, @RequestParam String token) {
//        userService.checkUser(token);
        if (null != id && id <= 0) {
            throw new BusinessException(120009);
        }
        channelService.delete(id);
        return response();
    }

    /**
     * 删除渠道一群
     * @param ids
     * @param token
     * @return
     */
    @GetMapping("deleteAll")
    public String deleteAll(@RequestParam List<Integer> ids, @RequestParam String token) {
//        userService.checkUser(token);
        if (CollectionUtils.isEmpty(ids)) {
            throw new BusinessException(120009);
        }
        channelService.deleteAll(ids);
        return response();
    }
}
